-
Notifications
You must be signed in to change notification settings - Fork 247
Update on the QuantModule & DynamicModule to accept external forward #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Jingyu Xin <jingyux@nvidia.com>
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
📝 WalkthroughWalkthroughThis PR implements a forward method preservation mechanism during dynamic module conversion and quantization. It adds logic to save the pre-monkey-patched forward method to a Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #824 +/- ##
==========================================
+ Coverage 73.82% 73.85% +0.02%
==========================================
Files 193 193
Lines 19745 19763 +18
==========================================
+ Hits 14577 14595 +18
Misses 5168 5168 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Signed-off-by: Jingyu Xin <jingyux@nvidia.com>
Signed-off-by: Jingyu Xin <jingyux@nvidia.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@tests/unit/torch/opt/test_chaining.py`:
- Around line 244-295: The patched_forward closure currently closes over the
outer variable model and is assigned to both model.linear.forward and
teacher_model.linear.forward, causing the teacher to use the student's weights
after apply_mode; fix by creating a bound forward for each linear instance
(e.g., a factory make_patched_forward(linear_module) that captures linear_module
and returns a function using linear_module.weight/bias, or bind the function to
the instance with types.MethodType) and assign model.linear.forward =
make_patched_forward(model.linear) and teacher_model.linear.forward =
make_patched_forward(teacher_model.linear) so each module uses its own weights
during test_sparse_quantize_kd_linear_forward_backward.
Signed-off-by: Jingyu Xin <jingyux@nvidia.com>
Edwardf0t1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
| # accelerate patched module | ||
| bind_forward_method(self, self.__class__.forward) | ||
| else: | ||
| # https://github.com/NVIDIA/Model-Optimizer/pull/824 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we may not need this comment as the IDE can show the code change is from which PR.
What does this PR do?
Type of change: new feature
Overview:
This MR improves robustness when
forward()is monkey‑patched (replaced at runtime) on modules that later get wrapped/converted by ModelOpt (DynamicModule + quant wrappers).It addresses two concrete failure modes introduced/exposed by supporting “patched forward” modules:
Forward “leakage” after export: a dynamic wrapper forward could remain bound on an instance even after export() restores the original (non‑dynamic) class, causing runtime errors in unrelated codepaths (e.g. KD export/save/restore chains).
Infinite recursion in quant wrappers: _forward_pre_dm can sometimes point to a wrapper forward that already participates in the class chain, causing a recursion loop when quant wrappers call _forward_pre_dm directly.
Usage
Testing
Before your PR is "Ready for review"
Additional Information
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.